package com.wen.task.customTask;

import com.wen.entry.Result;
import com.wen.entry.bean.DebrisCombineEquipment;
import com.wen.entry.bean.Equipment;
import com.wen.moudle.Account;
import com.wen.moudle.Managers.BackpackManager;
import com.wen.moudle.Managers.EquipmentManager;
import com.wen.task.AbstractTask;
import com.wen.utils.ConsoleUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;


// 变异任务,尽可能变异出需要的部位---完成
@Slf4j
public class VariationEquipmentTask extends AbstractTask {
    private EquipmentManager equipmentManager;
    private BackpackManager backpackManager;
    private List<Integer> type = new ArrayList<>();
    Account account;
    public VariationEquipmentTask(Account account) {
        super(account);
        this.account = account;
        backpackManager = getBackpackManager();
        equipmentManager = getEquipmentManager();
    }

    @Override
    public String getTitle() {
        return "消耗所有蓝碎片尽可能变异所需要部位";
    }

    @Override
    public void run() {
        boolean yes = false;
        Scanner in = new Scanner(System.in);
        while (!yes){
            type.clear();
            System.out.println("请输入你希望变异的部位");
            System.out.println("0、拳甲");
            System.out.println("1、头盔");
            System.out.println("2、短靴");
            System.out.println("3、铠甲");
            System.out.println("4、返回功能面板");
            System.out.println("请输入...【用空格分割】");

            String s = in.nextLine();
            if (s.charAt(0) == '4'){
                return;
            }
            Arrays.stream(s.trim().split("\\s"))
                    .mapToInt(Integer::parseInt)
                    .forEach(type::add);
            System.out.println(String.format("您将尝试合成 %s(y/n)?", type));
            String input = in.nextLine();
            yes = input.length() == 0 || input.charAt(0) != 'n';
        }
        variation();
    }


    public void variation(){
        System.out.println("是否自动卖掉融合后但不是狂的紫色装备?");
        boolean sold = ConsoleUtils.yesOrNo();
        Integer id = backpackManager.getGoodId("蓝碎片");
        List<Equipment> best = new ArrayList<>();
        int count = 0; // 合成失败次数
        while (true){

            Result<DebrisCombineEquipment> combine = backpackManager.combineGood(id);
            if (combine.isSuccess()){  // 可以融合成功，一直合成
                count = 0; // 合成成功，失败次数重置
                log.info("融合获得 {}", combine.getData().getRes());
                Equipment equipment = combine.getData().getEquipment();
                boolean nomatch = type.stream().noneMatch(e -> equipment.getType().equals(e));
                // 不是需要部位
                if (nomatch){
                    if (account.getDefaultPropertyConfig().blueTrash(equipment)){
                        // 垃圾属性，卖掉
                        equipmentManager.sold(equipment);
                        log.info("卖出 {}", equipment);
                    }else{
                        log.info("较好的属性 {},保留", equipment);
                    }
                }
            }else{
                if (count != 0) {
                    log.info("{}", combine.getErrorMessage());
                    break;// 如果不是第一次失败，退出
                }
                count++;  // 如果第一次失败，可能是钱不够或者碎片不够，合装备如果是垃圾装备，可以卖掉换钱

                // 获取蓝色装备，过滤出需要的部位
                ArrayDeque<Equipment> queue = equipmentManager.getBlueEquipment().stream().filter(e -> {
                    return type.stream().anyMatch(type -> e.getType().equals(type));
                }).collect(Collectors.toCollection(ArrayDeque::new));

                while (queue.size() >= 3){
                    Equipment e1 = queue.poll();
                    Equipment e2 = queue.poll();
                    Equipment e3 = queue.poll();
                    Equipment result = equipmentManager.combine(e1, e2, e3).getData().getInfo();
                    if (!result.getName().contains("狂战") && sold){  // 不是狂战并且允许卖掉
                        log.info("合成获得 {} ,不是狂战,卖掉", result);
                        equipmentManager.sold(result);
                    }else{
                        log.info("获得 {}", result);
                        best.add(result);
                    }
                }
            }
        }
        log.info("==========本次收获==========" );
        for (Equipment equipment : best) {
            log.info("{} ", equipment);
        }
    }

}
